<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/8/11
 * Time: 16:51
 */

namespace App\Service;

use App\Models\Menu;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;

class MenuService
{
    /**
     * 获取菜单列表
     * @param array $params
     * @param string $orderBy
     * @param string $sort
     * @param int $page
     * @param int $limit
     * @return \Illuminate\Http\JsonResponse
     */
    public static function getList($params = [],$orderBy = 'id',$sort = 'DESC')
    {
        $where = [];
//        $menuModel = new Menu();
        if(isset($params['title']) && empty($params['title'])){
            $where[] = ['title','like',"%{$params['title']}%"];
        }
        $list = Menu::where($where)->orderBy($orderBy,$sort)->get(['id', 'parent_id', 'order', 'title','created_at', 'updated_at' ])->toArray();
        $list = getTree($list);
        return Response()->json(['code'=>200,'msg'=>'获取成功','data'=>$list]);
    }

    /**
     * 通过ID获取菜单栏
     * @param int $menuId
     * @return array
     */
    public static function getMenuById($menuId = 0)
    {
        $info = Menu::where('id', $menuId)->first(['id', 'parent_id', 'order', 'title','created_at', 'updated_at']);
        $info = $info ? $info->toArray() : [];
        return Response()->json(['code' => 200, 'msg' => '获取成功', 'data' => $info]);

    }

    /**
     * 添加
     * @param array $params
     * @return \Illuminate\Http\JsonResponse
     */
    public static function add($params = [])
    {
        try{
            if(Menu::where('title',trim($params['title']))->where('parent_id',intval($params['parent_id']))->first()){
                throw new \Exception('该菜单已存在');
            }
            $info = Menu::create(['parent_id'=>intval($params['parent_id']),'title'=>trim($params['title']),'order'=>intval($params['order'])]);
            if(!$info){
                throw new \Exception('新增失败');
            }
        }catch (\Exception $e){
            return Response()->json(['code'=>201,'msg'=>$e->getMessage()]);
        }
        return Response()->json(['code'=>200,'msg'=>'新增成功']);
    }

    /**
     * 修改
     * @param array $params
     * @return \Illuminate\Http\JsonResponse
     */
    public static function edit($params = [])
    {
        try{
            $info = Menu::where('id',intval($params['id']))->first();
            if(!$info){
                throw new \Exception('该菜单不存在');
            }
            if(Menu::where('title',trim($params['title']))->where('parent_id',intval($params['parent_id']))->where('id','!=',intval($params['id']))->first()){
                throw new \Exception('该菜单名称已存在');
            }
            $info->title        = trim($params['title']);
            $info->parent_id    = intval($params['parent_id']);
            $info->order        = intval($params['order']);
            $save = $info->save();
            if(!$save) throw new \Exception('修改失败');
        }catch (\Exception $e){
            return Response()->json(['code'=>201,'msg'=>$e->getMessage()]);
        }
        return Response()->json(['code'=>200,'msg'=>'修改成功']);
    }

    /**
     * 删除
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public static function destroy($id)
    {
        $idArr = is_array($id) ? $id : [$id];
        if(!$idArr){
            return Response()->json(['code'=>201,'msg'=>'请选择要删除的菜单']);
        }
        $del = Menu::destroy($idArr);
        if(!$del){
            return Response()->json(['code'=>201,'msg'=>'删除失败']);
        }
        $list  = Menu::orderBy('order','DESC')->get(['id', 'parent_id', 'order', 'title'])->toArray();
        $sonArr = [];
        foreach($idArr as $v){
            $sonList = getSubs($v,$list);
            if($sonList){
                $sonArr = array_merge(array_column($sonList,'id'),$sonArr) ;
            }
        }
        if($sonArr){
            Menu::destroy($sonArr);
        }
        DB::table('admin_permission_menu')->whereIn('menu_id',array_merge($sonArr,$idArr))->delete();
        return Response()->json(['code'=>200,'msg'=>'删除成功']);
    }
}
